package com.littlebee.storychild.common.utils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;

import com.littlebee.storychild.R;


/**
 *
 * @ClassName: CommonUtil
 * @Description: 公共工具
 */
public final class CommonUtils {
    public static final int TIME_MILLISECOND_RESPONSE = 1366;
    private CommonUtils() {}

    /**
     * 从start界面跳转到dst界面
     *
     * @param actStart
     *            当前界面
     * @param dstActivityClz
     *            跳转到的界面
     */
    public static void openActivity(Activity actStart,
                                    Class<? extends Activity> dstActivityClz) {
        openActivity(actStart, dstActivityClz, null);
    }

    /**
     * 从start界面跳转到dst界面,携带点数据
     * @param actStart 当前界面
     * @param dstActivityClz 要跳转到的界面
     * @param savedData 携带的数据
     */
    public static void openActivity(Activity actStart,
                                    Class<? extends Activity> dstActivityClz, Bundle savedData) {
        Intent intent = new Intent(actStart, dstActivityClz);
        if (null != savedData) {
            intent.putExtras(savedData);
        }
        actStart.startActivity(intent);

    }

    /**
     * 加密身份证号，中间8位隐藏
     * @param idcard 身份证号
     */
    public static String StringFormatIdCard(String idcard) {
        if (TextUtils.isEmpty(idcard)) {
            return "";
        }
        return StringFormatIdCard(idcard, "*");
    }
    /**
     * 加密身份证号，中间8位隐藏
     * @param idcard 身份证号
     */
    public static String StringFormatIdCard(String idcard, String formatchar) {
        int count=18;
        int startcount=6;
        int endcount=4;

        if(idcard.length()>startcount) {
            String startchars = idcard.substring(0, startcount);
            String endchars = idcard.substring(idcard.length() - endcount, idcard.length());
            String formatchars="";
            for(int i=0;i<(count-startcount-endcount);i++){
                formatchars+=formatchar;
            }
            return (startchars + formatchars + endchars);
        }
        else{
            return idcard;
        }
    }

    /**
     * 加密手机号，中间4位隐藏
     * @param mobile 手机号
     */
    public static String StringFormatMobile(String mobile) {
        return StringFormatMobile(mobile,"*");
    }
    /**
     * 加密手机号，中间4位隐藏
     * @param mobile 手机号
     */
    public static String StringFormatMobile(String mobile, String formatchar) {
        if (null == mobile) {
            return "";
        }
        if(mobile.length()>4) {
            String first3 = mobile.substring(0, 3);
            String last4 = mobile.substring(mobile.length() - 4, mobile.length());
            return (first3 + formatchar+formatchar+formatchar+formatchar + last4);
        }
        else{
            return mobile;
        }
    }

    /**
     * 保存设置
     */
    public static void setSharedPreferences(Activity activity,
                                            String sharedpreferencesName, String key, Object value) {
        SharedPreferences preferences = activity.getSharedPreferences(
                sharedpreferencesName, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        // 存入数据
        if (value instanceof String) {
            editor.putString(key, (String)value);
        }

        if (value instanceof Integer) {
            editor.putInt(key, (int)value);
        }

        // 提交修改
        editor.commit();
    }


    static long[] mHits = new long[2];
    /**
     * 是否双击
     * @return true 是猛击
     */
    public static boolean isFast2Click() {
        System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
        mHits[mHits.length - 1] = SystemClock.uptimeMillis();
        if (mHits[0] >= (SystemClock.uptimeMillis() - TIME_MILLISECOND_RESPONSE)) {
            return true;
        }
        return false;
    }


    public static void showNoData(Activity activity, View hideView) {
        ViewUtil.hideView(hideView);
        ViewUtil.showView(activity.findViewById(R.id.view));
    }

    public static void showData(Activity activity, View showView) {
        ViewUtil.showView(showView);
        ViewUtil.hideView(activity.findViewById(R.id.view));
    }
}
